package cc.mrbird.febs.utils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CodeUtils {
    public static Set<String> getRuleCodeSet(String rule) {
        Set<String> codeSet = new HashSet<>();
        String pattern = "(?<=【)([^【】]*)(?=】)";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(rule);
        while (m.find()) {
            codeSet.add(m.group());
        }
        return codeSet;
    }

    public static String monthStyle(String monthStr) {
        if(Objects.equals(monthStr, "")) {
            return "";
        }
        String[] months = monthStr.split(",");
        List<String> result = new ArrayList<>();
        int start = Integer.parseInt(months[0]);
        if(months.length==1) {
            return months[0];
        }
        for(int i=1; i < months.length; i++ ) {
            if(Integer.parseInt(months[i]) - 1 != Integer.parseInt(months[i-1])) {
                int end = Integer.parseInt(months[i-1]);
                if (start==end) {
                    result.add(Integer.toString(start));
                } else {
                    result.add(Integer.toString(start).concat("-").concat(Integer.toString(end)));
                }
                start = Integer.parseInt(months[i]);
            }
        }
        if (start != Integer.parseInt(months[months.length - 1])) {
            result.add(Integer.toString(start).concat("-").concat(months[months.length - 1]));
        }
        if(months.length>=2) {
            if (!Objects.equals(Integer.parseInt(months[months.length - 1]) - 1, Integer.parseInt(months[months.length - 2]))) {
                result.add(months[months.length - 1]);
            }
        }
        return StringUtils.join(result,",");
    }

//    public static void main(String[] args) {
//        List<Integer> thirdList = new ArrayList<>();
//        thirdList.add(8);
//        thirdList.add(7);
//        Collections.sort(thirdList);
//        System.out.println(thirdList);
//    }
}
